package com.whf.android.jar.popup;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.popup.listener.OnClickDecisionListener;


/**
 * 弹框基层
 *
 * @author qf
 * @version 1.0.4
 **/
public abstract class LattePopupBottom extends BottomPopupView {

    protected OnClickDecisionListener mClickDecisionListener = null;

    public LattePopupBottom(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getMaxHeight() {
        return (int) (ScreenUtils.getAppScreenHeight() * 0.7f);
    }

    protected View getView(@IdRes int id) {
        return findViewById(id);
    }

    protected TextView getTextView(@IdRes int id) {
        return findViewById(id);
    }

    protected EditText getEditText(@IdRes int id) {
        return findViewById(id);
    }

    protected ImageView getImageView(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected Button getButton(@IdRes int id) {
        return findViewById(id);
    }

    //************************************* 重写界面 *****************************************/

    /*** 布局文件 */
    protected abstract int setLayout();

    /*** 初始化 */
    protected abstract void initViews();

    @Override
    protected int getImplLayoutId() {
        return setLayout();
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initViews();
    }

    /************************************* 设置值 *****************************************/

    protected final void setText(@IdRes int id, CharSequence text) {
        final TextView textView = getTextView(id);
        if (textView != null && !TextUtils.isEmpty(text)) {
            textView.setText(text);
        }
    }

    protected final void setTextHint(@IdRes int id, CharSequence hint) {
        final TextView editText = findViewById(id);
        if (editText != null && !TextUtils.isEmpty(hint)) {
            editText.setHint(hint);
        }
    }

    protected final void setTextColor(@IdRes int id, @ColorInt int color) {
        final TextView textView = findViewById(id);
        if (textView != null) {
            textView.setTextColor(color);
        }
    }

    protected final void setImageResource(int viewId, @DrawableRes int text) {
        ImageView view = getImageView(viewId);
        if (view != null) {
            view.setImageResource(text);
        }
    }

    protected final void setBackgroundResource(@IdRes int viewId, @DrawableRes int resId) {
        if (findViewById(viewId) != null) {
            findViewById(viewId).setBackgroundResource(resId);
        }
    }

    protected final void setBackgroundColor(@IdRes int viewId, @DrawableRes int resId) {
        if (findViewById(viewId) != null) {
            findViewById(viewId).setBackgroundColor(resId);
        }
    }

    /************************************* 设置控件是否显示  *****************************************/

    protected final void setShow(int viewId, boolean show) {
        final View view = getView(viewId);
        if (view != null) {
            view.setVisibility(show ? VISIBLE : GONE);
        }
    }

    /************************************* 设置获取值  *****************************************/

    protected String getTagStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        if (getTextView(viewId).getTag() == null) {
            return "";
        }
        return String.valueOf(getTextView(viewId).getTag()).trim();
    }

    protected String getHintStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        if (getTextView(viewId).getHint() == null) {
            return "";
        }
        return String.valueOf(getTextView(viewId).getHint()).trim();
    }

    protected String getTextStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        return getTextView(viewId).getText().toString().trim();
    }

    protected String getTextStr(TextView view) {
        if (view == null) {
            return "";
        }
        return view.getText().toString().trim();
    }

    /************************************* 设置事件  *****************************************/

    protected final void setOnClickListener(@IdRes int viewId, OnClickListener listener) {
        View view = findViewById(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
    }

    /**
     * 添加事件
     *
     * @param listener：事件
     */
    public LattePopupBottom setOnClickListener(OnClickDecisionListener listener) {
        this.mClickDecisionListener = listener;
        return this;
    }

    /**
     * 显示屏幕下方
     */
    public final void popup() {
        popup(false, false);
    }

    /**
     * 显示屏幕下方
     */
    public final void popup(boolean autoFocusEditText) {
        popup(autoFocusEditText, false);
    }

    /**
     * 显示屏幕下方
     * <p>是否让弹窗内的输入框自动获取焦点，默认是false。</p>
     * <p>点击外部与按返回键是否消失，默认是false。</p>
     */
    public final void popup(boolean autoFocusEditText, boolean dismiss) {
        KeyboardUtils.hideSoftInput(Latte.getActivity());
        new XPopup.Builder(Latte.getActivity())
                .autoFocusEditText(autoFocusEditText)
                .moveUpToKeyboard(autoFocusEditText)
                .dismissOnTouchOutside(dismiss)
                .dismissOnBackPressed(dismiss)
                .enableDrag(true)
                .isDestroyOnDismiss(false)
                .isThreeDrag(false)
                .asCustom(this)
                .show();
    }

}
